
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;
using System.Data.SqlTypes;
using Evaluation3.utils;
    public partial class admin_user : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {

                Session[keyUtils.SES_SELECTEDTAB] = "0";
            }
            

        }

        



       
        
       
        public void formReset()
        {
            mem_fullname.Text = null;
            mem_mail.Text = null;
            mem_phone.Text = null;
            mem_address.Value = null;
            mem_comp.Text = null;

        }
        public void inserUserData(SqlConnection conn, String mem_fullname, String mem_address, String mem_email, String mem_phone, String mem_comp)
        {

            String updateSql = "insert into guest (hovaten,diachi,hopthu,dienthoai,tencongty) values(@mem_fullname, @mem_address, @mem_email, @mem_phone,@mem_comp)";
            SqlCommand cmd = new SqlCommand(updateSql, conn);
            cmd.Parameters.Add(new SqlParameter("@mem_fullname", mem_fullname));           
            cmd.Parameters.Add(new SqlParameter("@mem_address", mem_address));
            cmd.Parameters.Add(new SqlParameter("@mem_email", mem_email));
            cmd.Parameters.Add(new SqlParameter("@mem_phone", mem_phone));
            cmd.Parameters.Add(new SqlParameter("@mem_comp", mem_comp));
            if (conn.State == ConnectionState.Closed) conn.Open();
            cmd.ExecuteNonQuery();
            conn.Close();

        }
        protected void ChangeTabIndex(object sender, EventArgs e)
        {
            String senderId = ((LinkButton)sender).ID;

            setSelectedTab(senderId.Substring(senderId.Length - 1));

        }
        protected String getSelectedTab() { return Session[keyUtils.SES_SELECTEDTAB].ToString(); }
        protected void setSelectedTab(String i) { Session[keyUtils.SES_SELECTEDTAB] = i; }

        public void deleteUserData(SqlConnection conn, String mem_id, String mem_name)
        {
            Evaluation3.utils.dataUtils.ExecuteQuery("delete guest where idguest=' " + mem_id + "'", CommandType.Text);
            UserList.DataBind();
        }

        public bool isValidate(SqlConnection conn, String email)
        {
            string sqlCount = "select count(*) from guest where hopthu='" + email + "'";
            SqlCommand cmdCount = new SqlCommand(sqlCount, conn);
            if (conn.State == ConnectionState.Closed) conn.Open();
            SqlDataReader rs = cmdCount.ExecuteReader();
            rs.Read();
            SqlInt32 dem = rs.GetSqlInt32(0);
            rs.Close();
            cmdCount.Dispose();
            conn.Close();
            if (dem > 0) return true;
            return false;
        }
        protected void btn_Save_Click(object sender, EventArgs e)
        {
            if (isValidate((SqlConnection)Application.Get("myConn"), mem_mail.Text))
            {
                CustomValidator1.Visible = true;
                CustomValidator1.IsValid = false;
                return;
            }
            inserUserData((SqlConnection)Application.Get("myConn"), mem_fullname.Text, mem_address.Value, mem_mail.Text, mem_phone.Text, mem_comp.Text);
           // BindGrid((SqlConnection)Application.Get("myConn"));
            UserList.DataBind();
            formReset();
            Session[keyUtils.SES_SELECTEDTAB] = "0";
        }
        protected void btn_Reset_Click(object sender, EventArgs e)
        {
            formReset();
        }
        


        protected void UserList_DeleteCommand(object source, DataGridCommandEventArgs e)
        {
            if (e.Item.ItemIndex > -1)
            {
                String mem_id = UserList.DataKeys[(int)e.Item.ItemIndex].ToString();
                String mem_name = e.Item.Cells[1].Text;

                
                deleteUserData((SqlConnection)Application.Get("myConn"), mem_id, mem_id);
                UserList.EditItemIndex = -1;
                UserList.SelectedIndex = -1;
                UserList.DataBind();
                //BindGrid((SqlConnection)Application.Get("myConn"));
            }
        }
        protected void UserList_EditCommand(object source, DataGridCommandEventArgs e)
        {
            String mem_id = UserList.DataKeys[(int)e.Item.ItemIndex].ToString();
            UserList.EditItemIndex = -1;
            UserList.SelectedIndex = -1;
             
            //BindGrid((SqlConnection)Application.Get("myConn"));
            Response.Redirect("admin_member.aspx?mem_id=" + mem_id);
        }
    }
